package com.dqx.haoqiu.mvp.module.guide;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BaseFragment;
import com.dqx.haoqiu.mvp.common.constant.IConstants;
import com.dqx.haoqiu.mvp.module.guide.dagger.DaggerGuideComponent;
import com.dqx.haoqiu.mvp.module.guide.dagger.GuideModule;
import com.dqx.haoqiu.mvp.utils.Navigation;
import com.xmjs.baselibrary.utils.SP;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dqx.haoqiu.mvp.common.base.BaseApplication.getAppComponent;

public class GuideItemFragment extends BaseFragment<GuidePresenter> implements GuideContract.View {
    @BindView(R.id.iv_bg)
    ImageView mIvBg;
    @BindView(R.id.tv_start)
    TextView mTvStart;

    public static GuideItemFragment newInstance(int imageRes, boolean isLastItem) {
        GuideItemFragment fragment = new GuideItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(IConstants.IMAGE_RES, imageRes);
        bundle.putBoolean(IConstants.IS_LAST_ITEM, isLastItem);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        DaggerGuideComponent.builder()
                .appComponent(getAppComponent())
                .guideModule(new GuideModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_guide_item, container, false);
        ButterKnife.bind(this, mView);

        //图片
        int imageRes = getArguments().getInt(IConstants.IMAGE_RES, 0);
        //是否是最后一页
        boolean showSkip = getArguments().getBoolean(IConstants.IS_LAST_ITEM, false);
        //显示图片
        Glide.with(getContext()).load(imageRes).into(mIvBg);
        //显示/隐藏跳转按钮
        mTvStart.setVisibility(showSkip ? View.VISIBLE : View.GONE);
        return mView;
    }

    @OnClick(R.id.tv_start)
    public void onClick() {
        //标记已经打开过引导页
        presenter.setShowGuide(false);
        Navigation.openLogin(getActivity());
        getActivity().finish();
    }

}
